package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.MessageBean;

import java.util.List;

/**
 * Class:系统消息适配器
 * Created by KANG on 2016/11/20  15:30.
 * Email:khl8912@126.com
 */

public class SystemMessageAdapter extends RecyclerView.Adapter<SystemMessageAdapter.SystemMessageHolder> {
    private Context context;
    private List<MessageBean> list_message;
    private SystemMessageAdapter.OnItemClickListener onItemClickListener;

    public SystemMessageAdapter(Context context, List<MessageBean> list_message) {
        this.context = context;
        this.list_message = list_message;
    }

    @Override
    public SystemMessageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_system_message, parent, false);
        return new SystemMessageAdapter.SystemMessageHolder(view, onItemClickListener);
    }

    @Override
    public void onBindViewHolder(SystemMessageHolder holder, int position) {
        final int pos = holder.getLayoutPosition();
        holder.tv_systemMessageTime.setText(list_message.get(pos).getSubmitTime());
        holder.tv_systemMessageTitle.setText(list_message.get(pos).getTitle());
        holder.tv_systemMessageContent.setText(list_message.get(pos).getContent());
    }

    @Override
    public int getItemCount() {
        return list_message != null ? list_message.size() : 0;
    }

    public void setOnItemClickListener(SystemMessageAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public static class SystemMessageHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private SystemMessageAdapter.OnItemClickListener onItemClickListener;

        private TextView tv_systemMessageTime;//时间
        private TextView tv_systemMessageTitle;//消息标题
        private TextView tv_systemMessageContent;//消息内容

        public SystemMessageHolder(View itemView, SystemMessageAdapter.OnItemClickListener onItemClickListener) {
            super(itemView);

            tv_systemMessageTime = (TextView) itemView.findViewById(R.id.tv_systemMessageTime);
            tv_systemMessageTitle = (TextView) itemView.findViewById(R.id.tv_systemMessageTitle);
            tv_systemMessageContent = (TextView) itemView.findViewById(R.id.tv_systemMessageContent);

            this.onItemClickListener = onItemClickListener;
            itemView.setOnClickListener(this);
        }


        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(getPosition());
            }
        }
    }
}
